package cn.south.campus.admin.campus.controller;

import cn.south.campus.admin.campus.model.req.ConfigReq;
import cn.south.campus.admin.campus.model.vo.ConfigVo;
import cn.south.campus.admin.campus.service.ConfigService;
import cn.south.campus.common.model.bo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 系统配置表 前端控制器
 * </p>
 *
 * @author chow
 * @since 2024-12-13
 */
@RestController
@RequestMapping("/campus/config")
@AllArgsConstructor
public class ConfigController {

    private final ConfigService configService;

    @GetMapping("pageQuery")
    public Result<IPage<ConfigVo>> pageQuery(ConfigReq req) {
        return Result.success(configService.pageQuery(req));
    }

    @PostMapping("/save")
    public Result<String> save(@RequestBody @Valid ConfigVo configVo) {
        configService.insert(configVo);
        return Result.success("保存成功");
    }

    @PutMapping("/update")
    public Result<String> update(@RequestBody @Valid ConfigVo configVo) {
        configService.modify(configVo);
        return Result.success("修改成功");
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id) {
        configService.delete(id);
        return Result.success("删除成功");
    }

    @GetMapping("refreshCache")
    public Result<String> refreshCache(@RequestParam String key) {
        configService.refreshCache(key);
        return Result.success("刷新成功");
    }

    @GetMapping("/get/{id}")
    public Result<ConfigVo> get(@PathVariable Long id) {
        return Result.success(configService.detail(id));
    }
}
